<?php
require('io/FileOp.php');
global $xml;
global $fileName;
$fileName = 'Main';
$xml=simplexml_load_file($fileName.'.form');

$content = $xml->NonVisualComponents->Menu->SubComponents;	
//print_r($content);
//ExtMenu();
HTMLForm();
//readResource('categoryOrderLabel.text');

function ExtMenu(){
	global $content;
	OpenFile('a.js');
	
	foreach($content->Menu as $menu){
		
		//print_r($menu);
		$properties = $menu->Properties->Property;
		
		
		$subcomponents = $menu->SubComponents;
		
		
		if($properties['name']=='label'){
			if($properties['type']=='java.lang.String'){
				$label = $properties['value'];
				//echo "The value is ".$properties['value'];
				
				/*************************************************************************/
				
				writeFile('var '.$label.'menu = new Ext.menu.Menu({');				
				writeFile('items: [');				
	            writeFile('{');				
	            $b = '		text: \'';
				$b.=$label;
				$b.='\',';
				writeFile($b);				
				writeFile('		menu: {');        				
	            writeFile('			items: [');				
				/********************************************************************////***********************************/
				
				foreach($subcomponents->MenuItem as $menuItem){
					
					$menuProperties = $menuItem->Properties->Property;
					//print_r($menuProperties);
					if($menuProperties['name']=='label'){
						if($menuProperties['type']=='java.lang.String'){
							$menuLabel = $menuProperties['value'];
							/**************///////////////////****************/
							
							writeFile('	{');							
							$b = '		text: \'';
							$b.=$menuLabel;
							$b.='\',';							
							writeFile($b);							
							writeFile('		handler: actionPermormed,');							
							writeFile('		},');							
							
							/*************///////////////////******************/
							
						}
					}				
				}			
				/***************************************************************************/
				
				writeFile('		]');				
				writeFile('	}');				
				writeFile('	}');				
				writeFile('	]');
				writeFile('});');				
					
			}
		}	
	}
}
function HTMLForm(){
	global $content, $xml;
	
	//print_r($xml);
	OpenFile('a.html');
	/************************** Form ***********************************/
	$subComponents = $xml->NonVisualComponents->Container->SubComponents;	
	//print_r($subComponents);
	foreach($subComponents->Component as $component){
		
		$property = $component->Properties->Property;
		switch($component['class']){		
			case 'java.awt.Label';			
				$label = $property['value'];
			break;
			case 'javax.swing.JLabel';
				$label = readResource($property['resourceKey']);				
			break;
			case 'java.awt.TextField';
			case 'javax.swing.JTextField':	
				$b = '<input name="'.$component['name'].'" type="text" value="';
				
				if($property['name']=='text'){
					$b.$property['value'];
				}
				$b.='" id="'.$component['name'].'" />';
				writeFile($label.$b);
			break;
			case 'java.awt.TextArea';
				writeFile('<textarea name="'.$component['name'].'">'.$property['value'].'</textarea>');
			break;
			case 'java.awt.Button':		
			case 'javax.swing.JButton':
				$eventHandler = $component->Events->EventHandler;
				$b = '<button name="'.$component['name'].'" onclick="return '.$eventHandler['handler'].'();">';			
				$b.=$label;
				writeFile($b);
				writeFile('</button>');			
				writeFile('<script type="text/javascript">');
				writeFile('function '.$eventHandler['handler'].'(){');
				writeFile('}');
				writeFile('</script>');
			break;
		}
		
	}
	CloseFile();
}

function readResource($resourceKey){
	global $fileName;
	$handle = fopen($fileName.".properties", "r");
	while ($line = fgets($handle)) {
		if(strcmp($line,$resourceKey)>5){
			$arr = explode('=',$line);
			return trim($arr[1]);
		}	    
	}
	fclose($handle);
}
?>